#! /usr/bin/env python
__author__ = "Alexander Merritt"
__email__ = "merritt.alex@gatech.edu"

import os
import commands
import sys

#Import all exported variables from SConstruct including 
#1) gcc or clang
Import('*')

#gcc = 'clang'

ccflags = ['-Wall', '-Wextra', '-Werror', '-Winline']
ccflags.extend(['-Wno-unused-parameter', '-Wno-unused-function'])

#cpath = ['../inc']
cpath = [os.getcwd() + '/../inc']
libpath.extend(['../lib'])
libs.extend(['ocm','m'])

if int(ARGUMENTS.get('debug', 0)): # 
    ccflags.append(['-ggdb', '-O0'])
    #Be careful using memcheck as it seems like it may affect some buffer used in the IB CM process.
    #This error shows up as the IB client trying to finish a connection and failing.
    #libs.append('mcheck')
else:
    ccflags.extend(['-O2'])

if compilepath == 'ib':
    ccflags.extend(['-DINFINIBAND'])
elif compilepath == 'extoll':
    ccflags.extend(['-DEXTOLL'])
    cpath.extend(['/extoll2/include'])
else:
    ccflags.extend(['-DINFINIBAND','-DEXTOLL'])
    cpath.extend(['/extoll2/include'])

#Add RMA libs if EXTOLL network is supported
#librma2 is located at /extoll2/lib/librma2.so
if compilepath == 'ib':
  extoll_libs = ['']
else:
  libs.extend(['librma2'])
  libpath.extend(['/extoll2/lib'])

env = Environment(CC = gcc, CCFLAGS = ccflags, CPPPATH = cpath)
env.Append(LIBPATH = libpath, LIBS = libs)

files = os.listdir('.')
for f in files:
    (name,ext) = os.path.splitext(f)
    if 'ib' in name and compilepath == 'extoll':
        continue
    elif 'extoll' in name and compilepath == 'ib':
        continue
    elif '.c' == ext.lower():
        bin = env.Program(name, [f])
